package cn.stylefeng.guns.modular.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.entity.IdjAgent;
import cn.stylefeng.guns.modular.entity.IdjUserRole;
import cn.stylefeng.guns.modular.mapper.IdjAgentMapper;
import cn.stylefeng.guns.modular.mapper.IdjUserRoleMapper;
import cn.stylefeng.guns.modular.service.IIdjAgentService;
import cn.stylefeng.guns.modular.util.StringUtil;
import cn.stylefeng.roses.core.util.ToolUtil;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

/**
 * <p>
 * 经纪人认证表 服务实现类
 * </p>
 *
 * @author TANGWJ
 * @since 2020-07-30
 */
@Service
public class IdjAgentServiceImpl extends ServiceImpl<IdjAgentMapper, IdjAgent> implements IIdjAgentService {

	@Autowired
	private IdjAgentMapper mapper;
	
	@Autowired
	private IdjUserRoleMapper roleMapper;
	
	@Override
	public LayuiPageInfo getList(Page<IdjAgent> page, String keyword, String beginTime, String endTime, String yes,
			String no, Integer isAudit) {
		Page<Map<String,Object>> list = mapper.getList(page, keyword, beginTime, endTime, yes, no, isAudit);
		return LayuiPageFactory.createPageInfo(list);
	}

	@Override
	public ModelAndView edit(Long agentId) {
		IdjAgent agent = mapper.selectByKeyId(agentId);
		if(agent != null){
			if(ToolUtil.isNotEmpty(agent.getCity()) && ToolUtil.isNotEmpty(agent.getProvince())){
				agent.setCityName(agent.getCity());
			}
			if(ToolUtil.isNotEmpty(agent.getRegion()) && ToolUtil.isNotEmpty(agent.getCityName())){
				agent.setCityName(agent.getCityName()+'-'+agent.getRegion());
			}else {
				agent.setCityName(agent.getProvince());
			}
			if(ToolUtil.isNotEmpty(agent.getGender())){
				if(agent.getGender() == 1){
					agent.setGenderName("男");
				}else if(agent.getGender() == 2){
					agent.setGenderName("女");
				}else{
					agent.setGenderName("未知");
				}
			}
			if(ToolUtil.isNotEmpty(agent.getReasonType())){
				if(agent.getReasonType() == 0){
					agent.setReasonTypeName("资料不全");
				}else{
					agent.setReasonTypeName("虚假信息");	
				}
			}
			if(ToolUtil.isNotEmpty(agent.getIsAudit())){
				if(agent.getIsAudit() == 1){
					agent.setReasonTypeName("已通过");
				}else{
					agent.setReasonTypeName("已拒绝");;	
				}
			}
			List<String> list = new ArrayList<String>();
			if(ToolUtil.isNotEmpty(agent.getAuditRejectionImg())){
				String[] split = agent.getAuditRejectionImg().split(",");
				for (int i = 0; i < split.length; i++) {
					list.add(split[i]);
				}
				agent.setImgList(list);
			}
		}
		ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("agent", agent);
        return modelAndView;
	}

	@Override
	public int setIsAudit(IdjAgent agent) {
		int i = mapper.setIsAudit(agent);
		if(agent.getIsAudit() == 1){
			IdjUserRole role = new IdjUserRole();
			role.setUserId(agent.getUserId());
			role.setUserRole(3);
			role.setCreateTime(new Date());
			roleMapper.insert(role);
		}
		return i;
	}

}
